﻿#include "ImageAcquisition.h"
#include "DVPCamera.h"

QImageAcquisition::QImageAcquisition(dvpHandle &handle, QObject *parent):
    QObject(parent)
{
    // 初始化成员变量
    m_handle = handle;
    pBuffer = NULL;

    m_timer = new QTimer();

    //将定时器超时信号与槽(功能函数)联系起来
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotGrabFrames()));

    //定时器每50毫秒采集一次，显示帧率大概维持在20帧每秒
    m_timer->start(50);
    //qDebug()<< __FUNCTION__ << " id = "<< QThread::currentThreadId();
}

QImageAcquisition::~QImageAcquisition()
{
    if(m_timer->isActive())
    {
        m_timer->stop();
        delete m_timer;
    }
}

bool QImageAcquisition::IsValidHandle(dvpHandle handle)
{
    bool bValidHandle = false;
    dvpIsValid(handle, &bValidHandle);

    return bValidHandle;
}

void QImageAcquisition::slotGrabFrames()
{
    dvpStatus status;
    status = dvpGetFrame(m_handle, &m_pFrame, &pBuffer, GRABTIMEOUT);

    if (status == DVP_STATUS_OK)
    {
        if(m_threadMutex.tryLock()){
            if(m_pFrame.format==FORMAT_BGR24){
                //其他版本先把BGR数据转成RGB数据，再用RGB数据转QImage
                m_ShowImage = QImage((uchar*)pBuffer,m_pFrame.iWidth, m_pFrame.iHeight, QImage::Format_RGB888); //5.7
                m_ShowImage = m_ShowImage.rgbSwapped();
            }else{
                m_ShowImage = QImage((uchar*)pBuffer,m_pFrame.iWidth, m_pFrame.iHeight,QImage::Format_Grayscale8); //5.7
            }
            m_threadMutex.unlock();
        }

        emit signalDisplay();
    }
}

